# coding:utf-8
"""
description:Home management》room management页面
"""
import os
import random
import time

from common.Log import LOG
from pages.HomeMgt_page import HomeManagement_page
from appium.webdriver.common import mobileby
from common.Gesture_Mainpulation import gesture_mainpulation

from common.Get_Yaml import get_yaml
from config.GlobalParameter import TEST_Element_YAML

testElements = get_yaml(os.path.join(TEST_Element_YAML, "RoomMgt.yaml"))


class RoomManagement_page(HomeManagement_page):
    by = mobileby.MobileBy()

    # 获取floor name，集合
    RoomMgmtFloorName_loc = (by.ID, testElements.get_elementInfo(0))

    def get_RoomMgmtFloorName(self):
        """获取floor name，集合"""
        return self.find_elements(*self.RoomMgmtFloorName_loc)

    # 获取room name,+room控件（最后一个）》pop-up，右上角save按钮元素见Public.yaml（3），集合
    RoomMgmtRoomName_loc = (by.ID, testElements.get_elementInfo(1))

    def get_RoomMgmtRoomName(self):
        """1.获取room name,+room控件（最后一个）》pop-up，右上角save按钮元素见Public.yaml（3），集合
        2.edit 状态，点击room控件进行删除，pop-up element元素见Public.yaml（3，13，14）"""
        return self.find_elements(*self.RoomMgmtRoomName_loc)

    def func_JumpDevicesPage(self, index):
        """
        点击floor下的room name，跳转页面.
        :param index:点击的room索引号
        :return: 根据index返回room name
        """
        super().click_HomeMgmtRoomMgmt()  # 进入Room management页面
        rn = self.get_RoomMgmtRoomName()[index]
        rnt = rn.text
        print(rnt)
        rn.click()  # 点击room name跳转页面
        return rnt

    # 点击右上角的edit按钮
    btnRoomMgmtEdit_loc = (by.ID, testElements.get_elementInfo(2))

    def click_btnRoomMgmtEdit(self):
        """点击右上角的edit按钮"""
        self.find_element(*self.btnRoomMgmtEdit_loc).click()

    def func_DelRoom(self, CheckText):
        """
        根据name删除room》关闭弹框》确认删除》断言room nums
        :param CheckText: room name
        :return: 未删room数量和删后数量对比，返回Ture和False
        """
        RoomNum = len(self.get_RoomMgmtRoomName())
        for index, row in enumerate(self.get_RoomMgmtRoomName()):
            if CheckText in row.text:
                self.get_RoomMgmtRoomName()[index].click()
                super().func_RandomXOrCancel()  # 关闭弹框
                self.get_RoomMgmtRoomName()[index].click()
                super().click_btnDeletePopupDelete()  # 再次弹框确认删除
                return RoomNum - 1 == len(self.get_RoomMgmtRoomName())
        LOG.info("根据name删除room，未找到指定room name，请检查指定room name...")
        return False

    # 第一个为Custom Room》pop-up element元素见Public.yaml（3，4，5），其他为固定默认room，集合
    availableRoomName_loc = (by.ID, testElements.get_elementInfo(4))

    def click_availableRoomName(self):
        """第一个为Custom Room，固定》pop-up element元素见Public.yaml（3，4，5），
        其他为固定默认room，集合"""
        self.find_elements(*self.availableRoomName_loc)[0].click()

    # 其他固定默认room右侧的单选框，集合
    cbAvailableRoom_loc = (by.ID, testElements.get_elementInfo(5))

    def get_cbAvailableRoom(self):
        """其他固定默认room右侧的单选框，集合"""
        return self.find_elements(*self.cbAvailableRoom_loc)

    def func_AddRoom(self, message):
        """
        添加room》随机点击room
        :param message:
        :return:
        """
        self.get_RoomMgmtRoomName()[-1].click()  # 点击+room控件（最后一个）

        random.choice(self.get_cbAvailableRoom()).click()
        super().click_ClosePopup()

        return super().find_toast(message)

    def func_AddCustomizeRoom(self, message, CheckName):
        """
        选择自定义room
        :param message:
        :param CheckName:
        :return:
        """
        self.get_RoomMgmtRoomName()[-1].click()  # 点击+room控件（最后一个）

        self.click_availableRoomName()  # 点击第一个customize room，固定
        super().func_input_name(CheckName)

        return super().find_toast(message)

    # 点击每个floor控件内的右上角...按钮
    btnRoomMgmtFloorEdit_loc = (by.ID, testElements.get_elementInfo(6))

    def click_btnRoomMgmtFloorEdit(self):
        """点击first floor控件内的右上角...按钮"""
        self.find_elements(*self.btnRoomMgmtFloorEdit_loc)[0].click()

    # rename floor,输入框
    PopupRenameFloor_loc = (by.ID, testElements.get_elementInfo(7))

    def input_PopupRenameFloor(self, RenameFloor):
        """rename floor,输入框"""
        self.send_keys(RenameFloor, self.find_element(*self.PopupRenameFloor_loc))

    # rename room name,输入框，集合
    PopupRenameFloorRoom_loc = (by.ID, testElements.get_elementInfo(8))

    def get_PopupRenameFloorRoom(self):
        """rename room name,输入框，集合"""
        return self.find_elements(*self.PopupRenameFloorRoom_loc)

    # rename  pop-up的ok按钮
    btnPopupRenameFloorOK_loc = (by.ID, testElements.get_elementInfo(9))

    def click_btnPopupRenameFloorOK(self):
        """rename  pop-up的ok按钮"""
        self.find_element(*self.btnPopupRenameFloorOK_loc).click()

    # 滑动rooms
    RoomMgmtFloors_loc = (by.ID, testElements.get_elementInfo(11))

    def swipe_RoomMgmtFloors(self):
        """滑动rooms"""
        gesture_mainpulation().swipe_up(self.driver, self.find_element(*self.RoomMgmtFloors_loc))

    def func_Rename(self, data, CheckText):
        """
        重命名name》选last floor》修改floor、随机room重命名
        :param data:RenameFloor: floor name.RoomsName: 随机数量的room name
        :param CheckText:
        :return:
        """
        if super().assertVisible(*self.btnRoomMgmtFloorEdit_loc):
            self.click_btnRoomMgmtFloorEdit()  # 点击first floor进行编辑
        self.input_PopupRenameFloor(data["RenameFloor"])
        for index, row in enumerate(data["RoomsName"]):
            self.send_keys(row, random.choice(self.get_PopupRenameFloorRoom()))
        self.click_btnPopupRenameFloorOK()
        return super().find_toast(CheckText)

    # 点击底部+floor按钮
    btnRoomMgmtAddFloor_loc = (by.ID, testElements.get_elementInfo(10))

    def click_btnRoomMgmtAddFloor(self):
        """点击底部+floor按钮"""
        MaxXY = gesture_mainpulation().get_sizes(self.driver)
        # 根据底部最大值y值推算滑动区域
        self.driver.swipe(MaxXY[0] - 500, MaxXY[1] - 100, MaxXY[0] - 500, MaxXY[1] - 1000, 200)
        self.find_element(*self.btnRoomMgmtAddFloor_loc).click()

    def func_AddFloor(self, Check, data):
        """
        添加floor》选择几个房间》修改floor和随机一个room name
        :param Check:
        :param data: 字典，SelectRoomNum 选中房间数量，RenameFloor，RoomsName
        :return:
        """
        self.click_homemgmtLine()  # side bar的HomeManagement
        self.click_HomeMgmtRoomMgmt()  # HomeManagement的Room Management
        self.click_btnRoomMgmtEdit()
        self.swipe_RoomMgmtFloors()  # 滑动Room management
        self.click_btnRoomMgmtAddFloor()
        for i in range(data["SelectRoomNum"]):
            random.choice(self.get_cbAvailableRoom()).click()
            super().swipe_designSheet()  # 滑动新建floor框
        super().click_PopupNext()  # 点击左上角next按钮
        return super().find_toast(Check)
